//
//  LadyBirdRequest.swift
//  eat_known
//
//  Created by 心情 on 2018/8/3.
//  Copyright © 2018年 心情. All rights reserved.
//

import Foundation
import Alamofire
import HandyJSON

//MARK:- 孕期提醒
class LadyBirdRequest: FoodRequestProtocol {
    static func start(keyword: String!, completion: @escaping (Array<Food>?, Error?) -> Void) {
        let urlStr = "https://api.ladybirdedu.com/app/v1/search?bbs_id=0&own_user_id=0&page=1&status=1&type=product&version=7.2.0&q=" + keyword.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
        Alamofire.request(urlStr).validate().responseString { (response) in
            switch response.result {
            case .failure(let error):
                completion(nil, error);
            case .success(let json):
                let foods = [Food].deserialize(from: json, designatedPath: "data.items")?.compactMap{$0}
                foods?.forEach({ (food) in
                    food.source = .LadyBird
                })
                completion(foods, nil)
            }
        }
    }
}
